1195C - Basketball Exercise - CodeForces Solution


dp *1400

Please click on ads to support us..

Python Code:

def main():
    import sys
    def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
    [n]=get_ints()
    g=[0]+get_ints()
    d=[0]+get_ints()
    zlicz=[[0 for _ in range(n+2)]for _ in range(3)]
    zlicz[1][1]=g[1]
    zlicz[2][1]=d[1]
    zlicz[0][2]=max(g[1],d[1])
    for i in range(2,n+1):
        zlicz[1][i]=g[i]+max(zlicz[2][i-1],zlicz[0][i-1])
        zlicz[2][i]=d[i]+max(zlicz[1][i-1],zlicz[0][i-1])
        zlicz[0][i]=max(zlicz[1][i-1],zlicz[2][i-1])
    print(max(zlicz[0][-2],zlicz[1][-2],zlicz[2][-2]))
main()

C++ Code:

#include <bits/stdc++.h> 
 
#define int long long 
#define pii pair <int,int> 
#define st first 
#define nd second 
#define vi vector <int> 
#define vpii vector <pii> 
#define pb push_back 
#define pf push_front 
#define FOR(a, b, c) for (int i = a; i <= b; i += c) 
#define ROS(a, b, c) for (int i = a; i >= b; i -= c) 
#define lb lower_bound 
#define ub upper_bound 
#define FILE "" 
#define trii pair <int, pii> 
using namespace std;
 
const int oo = 1e18;
const int N = 3e5 + 1;
const int mod = 1e9 + 7;
const int blocks = 448;
 
int n, a[N], b[N];
int dp[N][3];
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen(FILE".inp","r", stdin);
	//freopen(FILE".out","w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	
	for (int i = 1; i <= n; i++) 
		cin >> b[i];
	dp[0][0] = 0;
	dp[0][1] = 0;
	dp[0][2] = 0;
	for (int i = 1; i <= n; i++) 
	{
		dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1], dp[i - 1][2]));
		dp[i][1] = max(dp[i - 1][0] + a[i], dp[i - 1][2] + a[i]);
		dp[i][2] = max(dp[i - 1][0] + b[i], dp[i - 1][1] + b[i]);
	}
	cout << max(dp[n][0], max(dp[n][1], dp[n][2]));
	return 0;
}


Comments

Submit
0 Comments
More Questions

485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain
1611F - ATM and Students
660A - Co-prime Array
1692F - 3SUM
1470A - Strange Birthday Party
190D - Non-Secret Cypher
1721B - Deadly Laser
1721C - Min-Max Array Transformation
1721A - Image
1180C - Valeriy and Deque
557A - Ilya and Diplomas
1037D - Valid BFS
1144F - Graph Without Long Directed Paths
1228A - Distinct Digits
355B - Vasya and Public Transport
1230A - Dawid and Bags of Candies
1530A - Binary Decimal
1472D - Even-Odd Game
441C - Valera and Tubes
1328E - Tree Queries
265A - Colorful Stones (Simplified Edition)
296A - Yaroslav and Permutations
967B - Watering System
152A - Marks
1398A - Bad Triangle
137A - Postcards and photos